<template>
	<view class="content">

		<!-- <view class="item_div" @click.stop="onScanCode">
			<image class="logo" src="/static/ic_scan_code.png"></image>
			<view>{{hint}}</view>
		</view> -->

		<view class="info_div">
			<view class="item_div">
				<view>优惠券 |</view>
				<view class="et" @click.stop="onScanCode(1)">点击扫描优惠券码 ></view>
			</view>
		</view>

		<view class="scan_info">{{coupon_info}}</view>

		<view class="info_div">
			<view class="item_div">
				<view>托运单 |</view>
				<view class="et" @click.stop="onScanCode(2)">点击扫描托运单码 ></view>
			</view>
		</view>

		<view class="scan_info">{{order_info}}</view>

		<view class="info_div">
			<view class="item_div">
				<view style="display: flex;">
					<view style="color: red;">*</view>运费 |
				</view>
				<input class="et" type="digit" placeholder="请输入运费" @input="onInputFee" :value="fee" />
			</view>
		</view>

		<view id="btn_ok" @click.stop="onOk">核销</view>

	</view>
</template>

<script>
	import { getUInfo } from '@/util/util.js'
	export default {
		data() {
			return {
				hint: '请扫描用户点击优惠券展示的二维码',

				fee: '', // 用户输入的运费

				coupon_info: '',
				order_info: '',

				coupon_obj: {}, // 记录优惠券码对象
				order_obj: {}, // 记录托运单码对象

			}
		},

		onLoad() {

		},

		onShow() {
			// uni.showToast({ icon: 'none', title: '2025.0228.11', })
		},

		mounted() {
			let that = this

			if (!getUInfo() || !getUInfo().token) {
				uni.redirectTo({ url: '/pages/login/login' })
			}
			// 事件总线:收到扫码事件
			uni.$on('onGetQrCode', data => {
				// console.log('监听到事件来自扫码，携带参数为：' + data);
				if (data.type == 1) { // 收到优惠券扫码结果
					that.coupon_obj = JSON.parse(data.qr_code_data)
					that.coupon_info = `面值:¥${that.coupon_obj.money} 有效期至:${that.coupon_obj.end_time_text}`
				} else if (data.type == 2) { // 收到托运单码扫描结果
					that.order_obj = JSON.parse(data.qr_code_data)
					that.order_info = `发站:${that.order_obj.startCity} 到站:${that.order_obj.toCity} 单号:${that.order_obj.od_num}`
				}
			})

		},

		methods: {

			// 输入运费
			onInputFee(e) {
				this.fee = e.detail.value
			},

			// 测试核销
			testHexiao() {
				let that = this
				that.coupon_obj = { coupon_code: '6pPBIQifi4' }
				that.order_obj = { toCity: '黑龙江省佳木斯市富锦市', startCity: '山东省临沂市', goods_info: '力诚50件', od_num: 'zs250222281', qr_code: 'http://t800.chemanman.com/index.php?qr=69556_643929171488' }
				that.reqHexiao()
			},

			// onOk
			onOk() {
				// this.testHexiao()
				let that = this
				if (!this.fee || parseFloat(this.fee) <= 0) {
					uni.showToast({ icon: 'none', title: '请输入运费' })
					return
				}
				if (!that.coupon_info) {
					uni.showToast({ icon: 'none', title: '请扫描优惠券码' })
					return
				}
				if (!that.order_info) {
					uni.showToast({ icon: 'none', title: '请扫描托运单码' })
					return
				}
				uni.showModal({
					title: '提示',
					content: '是否确认核销？',
					success: res => {
						if (res.confirm) {
							that.reqHexiao()
						}
					}
				})
			},

			onScanCode(type) {
				uni.navigateTo({ url: `/pages/scan_qrcode/scan_qrcode?type=${type}` })

				// this.qr_code_data = 'jIna7yka'
				// this.reqHexiao()
			},

			// 接口:对优惠券进行核销
			async reqHexiao() {
				let that = this

				let token = getUInfo().token
				let coupon_code = that.coupon_obj.coupon_code
				let arrive = that.order_obj.toCity
				let send = that.order_obj.startCity
				let goods = that.order_obj.goods_info
				let sn = that.order_obj.od_num
				let qr_code = that.order_obj.qr_code
				let freight = this.fee
				uni.showLoading({ title: '处理中', mask: true, })
				let result = await that.$hexiao({ token, coupon_code, arrive, send, goods, sn, qr_code, freight })
				// console.log(`reqHexiao...code=${code},msg=${msg},data=`, data)
				if (result.code == 1) {
					// 核销成功 清空信息并弹窗提示
					that.coupon_info = ''
					that.coupon_obj = {}
					that.order_info = ''
					that.order_obj = {}
					that.fee = ''
					uni.showModal({
						title: '提示',
						content: `${result.msg}`,
						showCancel: false,
						success: res => {
							if (res.confirm) {

							}
						}
					})
				} else {
					uni.showModal({
						title: '提示',
						content: `核销失败！${result.msg}`,
						showCancel: false,
						success: res => {
							if (res.confirm) {

							}
						}
					})
				}

			},

		}
	}
</script>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.info_div {
		display: flex;
		flex-direction: column;
		width: calc(100% - 40rpx);
		margin: 20rpx 20rpx 0 20rpx;
	}

	/* 每个填写项目的容器 */
	.item_div {
		display: flex;
		justify-content: space-between;
		width: 100%;
		height: 60rpx;
		line-height: 60rpx;
		margin: 22rpx 0 0 0;
		font-size: 30rpx;
		/* background-color: red; */
		overflow: hidden;
	}

	/* 展示扫码内容 */
	.scan_info {
		align-self: flex-end;
		color: darkgray;
		margin: 0 20rpx 0 25%;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.scan_div {
		display: flex;
		flex-direction: column;
		align-items: center;
		font-size: 28rpx;
		color: rgba(1, 1, 1, 0.5);
	}

	.et {
		/* background-color: red; */
		flex-grow: 1;
		margin: 0 0 0 0rpx;
		max-width: 70%;
		height: 60rpx;
		text-align: right;
		font-size: 30rpx;
	}

	#btn_ok {
		width: 75%;
		height: 76rpx;
		line-height: 76rpx;
		text-align: center;
		background-color: deepskyblue;
		color: white;
		margin: 140rpx 0 0 20rpx;
		border-radius: 10rpx;
	}
</style>